/**
 * com.tshoe.commons.util
 * commons
 * SignUtils
 * 2019年8月22日
 */
package com.wceManage.commons.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;

/**
 * 签名工具类
 * 
 * @author zhaofei
 * @date 2019年8月22日
 */
public class SignUtils {

	/**
	 * 获取header信息
	 * 
	 * @param reqStr
	 * @return
	 */
	public static Map<String, String> getHeaderMap(String signature, String requestTime) {
		Map<String, String> header = new HashMap<String, String>();
		header.put("Authorization", "Bearer sk_live_5mrXr5irrr1SmzD4i91iv944");
		header.put("Pingplusplus-Signature", signature);
		header.put("Pingplusplus-Request-Timestamp", requestTime);
		header.put("Content-Type", "application/json");

		return header;
	}

	/**
	 * 生成签名
	 * 
	 * @param requestBody
	 * @param requestUri
	 * @param requestTime
	 * @return
	 */
	public static String getSignature(String requestBody, String requestUri, String requestTime) {
		
		// 请求时间
		// String requestTime = Integer.valueOf((int) (System.currentTimeMillis() /
		// 1000)).toString();
		// String requestBody = "<请求内容>";
		// 请求 URI（包括 URL 参数）
		// String requestUri = "/income/v1/order_sync";
		// 拼接待签名字符串
		String data = requestBody + requestUri + requestTime;
		String PEMEncodedPrivateKey = "-----BEGIN PRIVATE KEY-----\r\n"
				+ "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDD7NpsP7ZTU3hg\r\n"
				+ "BdNHRpvnw+ARzXcZHklM0BiBW14bJhTXnSJmjobwwDDfvlMYR+HuUxF1z83hgONk\r\n"
				+ "vWG8apmLk79eFapjmSF+6dbjZXA+EAjTEKEp2C5rXvMQdbENHc7rJKSzMq/EBoxo\r\n"
				+ "00tHLnUQQaoY+js5Oll2ZALBZb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VjB9vG\r\n"
				+ "k6bXavSlYz4edX3Hq96QyudQLSzKSBYyfIsgJpGRu5wNgDRYEWbjY0WabnoRDj27\r\n"
				+ "KqVi+Q2zd3x+/reRhRrf+p/F6bmbgW1Qsung84g2YdPIZm5EXzf0J6ac2QKBgQDM\r\n"
				+ "AekKV/48fyH7Cc/6S6dHvibfRKYDMAHxVU0Mwg9XlVaXTuGjgtuEZTKolu8/AJB0\r\n"
				+ "5edqCZdor2lInG7quHuZfGevd+8HBrNwHWaKcM/i0Jq7dO1AVj7s4ZYU9XfcPUb8\r\n"
				+ "IV+BLKnFBx/2rcb+XmKcP6UiheHBJAmIvWpdTMbgzQKBgHs2V4Yp3MCEtwhJnnoZ\r\n"
				+ "+s3EV4qZZqcFXytD37e8kp+o+x9WLDmDhgkf2ZU4DeJY0GkG8T0H+TjkOXjCU5Hi\r\n"
				+ "hubHagO5ZyrAYmvsIhFjHmcAhAaUGVAgOPVA8Dlqy/8zle5jTpbgU3xWV7YV+/Z6\r\n"
				+ "I7aMR7ZFylz52utEurnviFVV\r\n" + "-----END PRIVATE KEY-----";
		PEMEncodedPrivateKey = PEMEncodedPrivateKey.replaceAll("(-+BEGIN (RSA )?PRIVATE KEY-+\\r?\\n|-+END (RSA )?PRIVATE KEY-+\\r?\\n?)", "");
		byte[] privateKeyBytes = Base64.decodeBase64(PEMEncodedPrivateKey);
		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
		KeyFactory kf = null;
		PrivateKey privateKey = null;
		Signature signature = null;
		byte[] signBytes = null;
		try {
			kf = KeyFactory.getInstance("RSA");
			privateKey = kf.generatePrivate(keySpec);
			signature = Signature.getInstance("SHA256withRSA");
			signature.initSign(privateKey);
			signature.update(data.getBytes("UTF-8"));
			signBytes = signature.sign();
			// 生成的签名
			return Base64.encodeBase64String(signBytes).replaceAll("\n|\r", "");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (SignatureException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 元转分，确保price保留两位有效数字
	 * 
	 * @return
	 */
	public static int changeY2F(double price) {
		DecimalFormat df = new DecimalFormat("#.00");
		price = Double.valueOf(df.format(price));
		int money = (int) (price * 100);
		return money;
	}

}
